<?php
namespace app\admin\controller;
use think\Controller;
use auth\Auth;
use think\Session;
use think\Cookie;
use think\Db;
use think\Cache;
class Common extends Controller
{

	public function _initialize()
	{
		//判断用户是否登陆
		if(!session('uid','','admin')){
			$this->error('请先登陆系统！','Login/login');
		}
		//模块名称
        $module=request()->module();
        //控制器名称
    	$con=request()->controller();
    	//当前方法名称
		$action=request()->action();
        //当前规则name
        $name=$module.'/'.$con.'/'.$action;
        //exit($name);
        //获取后台面包屑导航
        $nav3=db('auth_rule')->field('title,pid')->where('name',$name)->find();
        $nav2=db('auth_rule')->field('title,pid')->where('id',$nav3['pid'])->find();
        $nav1='首页';
        $nav=$nav1.'&nbsp;&nbsp;/&nbsp;&nbsp;'.$nav2['title'].'&nbsp;&nbsp;/&nbsp;&nbsp;'.$nav3['title'];

        $this->assign([
            'con'=>$con,
            'nav'=>$nav
        ]);
        //auth权限认证
        //定义需要排除的方法
        $not_check = array('admin/Index/index','admin/Index/welcome','admin/Index/welcome_','admin/Index/welcome4','admin/Index/welcome3','admin/Index/uploadavatar','admin/Index/test');

        //判断当前控制器方法是否在定义排除的里面
        if(in_array($name, $not_check)){
            return true;
        }
        $auth=new Auth();

        if(!$auth->check($name,session('uid','','admin')) && session('uid','','admin') != 1){
            $this->error('没有访问权限！','');
        }

	}


        
        
    function getStaffTransferData($ids = '')
    {
        $g5List = db('admin')->field('group_members')->where('group_id','5')->select();
        $list = db('admin')->where('group_id','2')->orderRaw("CONVERT ('name' USING 'gbk')")->select();
        $groupMembers;
        foreach ($g5List as $k => $v) 
        {
            $groupMembers .= $v['group_members'].',';
        }
        $groupMembers = explode(',', substr($groupMembers, 0, -1));
        $idsArr = $ids?explode(',', $ids):[];
        $arr = [];
        foreach ($list as $k => $v) 
        {
            $arr[$k]['value'] = $v['id'];
            $arr[$k]['title'] = $v['name'];
            if(in_array($v['id'], $groupMembers) && !in_array($v['id'], $idsArr))
            {
                $arr[$k]['disabled'] = true;
            }
            else
                $arr[$k]['disabled'] = false;
        }
        $disabled = array_column($arr,'disabled');
        array_multisort($disabled,SORT_ASC,$arr);
        //var_dump($arr);
        return json_encode($arr,JSON_UNESCAPED_UNICODE);

    }

    function getPhonesByFormData($data)
    {
        $phones = [];
        for ($i=0; $i < 10; $i++) 
        { 
            if(trim($data['phone['.$i]))
            {
                $phones[] = trim($data['phone['.$i]);
            }
        }
        return $phones;
    }
    
    public function getSaleDeptJobs(){
        //$cache = Cache::get('sale_dept_jobs');
        if(!$cache)
        {
            $list = db('sale_dept_job')->field('id,name')->order('id ASC')->select();
            $arr = [];
            foreach ($list as $k=>$v)
            {
                $arr[$v['id']] = $v;
            }
            Cache::set('sale_dept_job',$arr);
            return $arr;
        }
        else
            return $cache;
    }

    public  function apiSuccess($data = 'success')
    {
        if(is_string($data)){
            $data = [
                'code' => 0,
                'data' => [],
                'message' => $data,
            ];
        }

        return json($data);
    }

    public  function apiError($message = '')
    {
        return json(['message' => $message, 'code' => -1]);
    }

}